package ar.com.ford.it.fullEconomics.persistence.dao.query;

import java.sql.Timestamp;
import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import org.apache.commons.lang.StringUtils;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.fullEconomics.struct.AgreementFindStruct;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class AgreemItemsFindQuery implements CustomExpressionFactory {

    public Expression createExpression(ExpressionBuilder builder,
                                       Object[] parameters) {
        Expression exp = null;
        AgreementFindStruct findStruct = (AgreementFindStruct) parameters[0];

        Teesg01Plant plant = findStruct.getPlant();
        String supplierClass = findStruct.getSupplierClass();
        String supplierCode = findStruct.getSupplierCode();
        String supplierName = findStruct.getSupplierName();
        Integer commodity = findStruct.getCommodity();
        String partClass = findStruct.getPartClass();
        String prefix = findStruct.getPrefix();
        String base = findStruct.getBase();
        String suffix = findStruct.getSuffix();
        String dateField = findStruct.getDateField();
        Timestamp dateFrom = findStruct.getDateFrom();
        Timestamp dateTo = findStruct.getDateTo();
        String userCode = findStruct.getUserCode();

        exp = builder.get("eesr04PlantCodeC").equal(plant);
        if (supplierClass != null && !supplierClass.equals("-1")) {
            exp = exp.and(builder.get("teesr03Agreement")
                    .get("teess01Supplier").get("eess01SupplierClassCodeC")
                    .equal(supplierClass));
        }
        if (supplierCode != null) {
            supplierCode += "%";
            exp = exp.and(builder.get("teesr03Agreement")
                    .get("teess01Supplier").get("eess01SupplierCodeC").like(
                            supplierCode));
        }
        if (supplierName != null) {
            supplierName += "%";
            exp = exp.and(builder.get("teesr03Agreement")
                    .get("teess01Supplier").get("eess01SupplierNameN").like(
                            supplierName));
        }

        if (dateField.equals("I")) {
            exp = exp.and(builder.get("teesr03Agreement").get(
                    "eesr03AgreeIssueDateY").between(dateFrom, dateTo));
        } else if (dateField.equals("S")) {
            exp = exp.and(builder.get("teesr03Agreement").get(
                    "eesr03AgreeSignatureDateY").between(dateFrom, dateTo));
        } else if (dateField.equals("R")) {
            exp = exp.and(builder.get("teesr03Agreement").get("eesr03CreateS")
                    .between(dateFrom, dateTo));
        } else if (dateField.equals("E")) {
            exp = exp.and(builder.get("teesr03Agreement").get(
                    "eesr03AgreeEffectiveDateY").between(dateFrom, dateTo));
        }
        if (userCode != null && !StringUtils.isBlank(userCode))
            exp = exp.and(builder.get("teesr03Agreement").get(
                    "eesr03CreateUserC").equal(userCode));

        if (partClass != null && !partClass.equals("-1")) {
            exp = exp.and(builder.get("teesp04Supply").get("teesp01Part").get(
                    "eesp01PartClassCodeC").equal(partClass));
        }
        if (prefix != null && !StringUtils.isBlank(prefix)) {
            prefix += "%";
            exp = exp.and(builder.get("teesp04Supply").get("teesp01Part").get(
                    "eesp01PartPrefixCodeC").like(prefix));
        }
        if (base != null && !StringUtils.isBlank(base)) {
            base += "%";
            exp = exp.and(builder.get("teesp04Supply").get("teesp01Part").get(
                    "eesp01PartBaseCodeC").like(base));
        }
        if (suffix != null && !StringUtils.isBlank(suffix)) {
            suffix += "%";
            exp = exp.and(builder.get("teesp04Supply").get("teesp01Part").get(
                    "eesp01PartSuffixCodeC").like(suffix));
        }
        if (commodity != -1) {
            exp = exp.and(builder.get("teesp02Commodity").get(
                    "eesp02CommodityNumberK").equal(commodity));
        }
        

        return exp;
    }
}
